using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.SharePoint;
using Sapphire.Linq.Framework;

namespace Sapphire.Linq.Mappers
{
    public class SPFieldLookupValueCollectionMapper : EnumerableMapper<SPFieldLookupValue>
    {
        public override IEnumerable<TTo> MapEnumerable<TTo>(IEnumerable<SPFieldLookupValue> fieldValueLookups, params object[] parameters)
        {
            if (fieldValueLookups.Count() == 0)
                return new List<TTo>();

            var queryProvider = parameters.OfType<LinqQueryProvider>().First();

            var field = parameters.OfType<SPFieldLookup>().First();

            var listQuery = queryProvider.GetList<TTo>(new Guid(field.LookupList));

            var whereLambda = fieldValueLookups.GetByIds<TTo>();

            return listQuery.Where(whereLambda);
        }
    }
}